package com.framwork.redisson.starter;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ysw
 * @create 2019-10-14 17:38
 * @descreption
 **/
@Configuration
@EnableConfigurationProperties(RedissonProperties.class)
@ConditionalOnProperty(prefix = "spring.redisson",value ="enabled", matchIfMissing = true)
@ConditionalOnClass(RedissonClient.class)
public class RedissonClientAutoConfiguration {

    @Autowired
    private RedissonProperties properties;

    @Bean
    @ConditionalOnMissingBean(RedissonClient.class)
    public RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress(properties.getUrl()).setDatabase(properties.getDataBase()).setPassword(properties.getPassword());
        config.setCodec(new JsonJacksonCodec());
        config.setThreads(32);
        config.setNettyThreads(32);
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}